package com.github.aloxc.plugin.restplus.method.action;

import com.github.aloxc.plugin.restplus.common.Consts;
import com.github.aloxc.plugin.restplus.common.PsiClassHelper;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.intellij.codeInsight.daemon.LineMarkerInfo;
import com.intellij.codeInsight.daemon.LineMarkerProvider;
import com.intellij.openapi.editor.markup.GutterIconRenderer;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiMethod;
import com.intellij.util.Function;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.asJava.LightClassUtilsKt;
import org.jetbrains.kotlin.psi.KtClass;
import org.jetbrains.kotlin.psi.KtNamedFunction;

import java.util.List;

/**
 * 在文件内容左边的竖栏上显示运行按钮
 */
public class PushYapiLineMarkerProvider implements LineMarkerProvider {

    private static Function<PsiMethod, String> YAPI_METHOD_TIPS = new Function<PsiMethod, String>() {
        @Override
        public String fun(PsiMethod psiMethod) {
            return I18Bundle.getMessage("com.github.aloxc.plugin.restplus.request.pushyapi.text");
        }
    };

    private static Function<PsiClass, String> YAPI_CLASS_TIPS = new Function<PsiClass, String>() {
        @Override
        public String fun(PsiClass psiClass) {
            return I18Bundle.getMessage("com.github.aloxc.plugin.restplus.request.pushyapi.text");
        }
    };

    @Nullable
    @Override
    public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement psiElement) {
        LineMarkerInfo lineMarkerInfo = null;
        boolean isRest = false;
        PsiMethod method = null;
        PsiClass clazz = null;
        if (psiElement instanceof PsiMethod) {
            PsiMethod psiMethod = (PsiMethod) psiElement;
            // rest method 或标注了RestController 注解
            isRest = PsiClassHelper.isRestController(psiMethod.getContainingClass());
            method = isRest ? psiMethod : null;
        } else if (psiElement instanceof KtNamedFunction) {
            List<PsiMethod> psiMethods = LightClassUtilsKt.toLightMethods(psiElement);
            for (PsiMethod psiMethod : psiMethods) {
                isRest = PsiClassHelper.isRestController(psiMethod.getContainingClass());
                if (isRest) {
                    break;
                }
            }
            method = isRest ? psiMethods.get(0) : null;
        } else if (psiElement instanceof PsiClass) {
            PsiClass psiClass = (PsiClass) psiElement;
            isRest = PsiClassHelper.isRestController(psiClass);
            clazz = isRest ? psiClass : null;
        } else if (psiElement instanceof KtClass) {
            KtClass ktClass = (KtClass) psiElement;
            PsiClass psiClass = LightClassUtilsKt.toLightClass(ktClass);
            isRest = psiClass != null && PsiClassHelper.isRestController(psiClass);
            clazz = isRest ? psiClass : null;
        } else {
            return lineMarkerInfo;
        }

        if (isRest) {
            try {
//                KtNamedFunction fun = (KtNamedFunction) psiElement;
//                List<PsiMethod> psiMethods = LightClassUtilsKt.toLightMethods(psiElement);
//                if (parentPsi instanceof KtClassOrObject) {
//                    KtLightClass ktLightClass = LightClassUtilsKt.toLightClass(((KtClassOrObject) parentPsi));
                lineMarkerInfo = new LineMarkerInfo(clazz == null ? method : clazz , clazz == null ? method.getNameIdentifier().getTextRange() : clazz.getNameIdentifier().getTextRange(), Consts.Icons.PUSH_YAPI,
                        clazz == null ? YAPI_METHOD_TIPS : YAPI_CLASS_TIPS,
                        new PushYapiLineMarkerHandler(),
                        GutterIconRenderer.Alignment.RIGHT);
            } catch (Exception exe) {
                //可能该类还未初始化完成
            }
        }
        return lineMarkerInfo;
    }
}
